//
//  VEHomeViewController.m
//  VEMoneyPorject
//
//  Created by BlueWind on 2018/9/20.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "VEHomeViewController.h"
#import "DTLogInViewController.h"

@interface VEHomeViewController ()
/**
 0 上线中，1 已上线
 */
@property (nonatomic, strong) NSString *onlineSatus;
@end

@implementation VEHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = self.model.ARTICLETITLE;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    UIWebView * webView = [[UIWebView alloc] init];
    webView.backgroundColor = [UIColor whiteColor];
    webView.scrollView.showsVerticalScrollIndicator = NO;
    webView.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(GAP15);
        make.left.equalTo(self.view).offset(GAP15);
        make.right.equalTo(self.view).offset(-GAP15);
        make.bottom.equalTo(self.view).offset(-49);
    }];
    UIButton *button = [UIButton new];
    button.hidden = YES;
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(webView.mas_bottom);
        make.left.equalTo(self.view).offset(GAP15 * 2);
        make.right.equalTo(self.view).offset(-GAP15 * 2);
        make.height.mas_equalTo(36);
    }];
    [button setBackgroundColor:kCommonRedColor];
    [button setTitle:@"分享" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(shareAction) forControlEvents:(UIControlEventTouchUpInside)];
    [button setLayerCornerRadius:18];
    
    CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:[NSString stringWithFormat:@"%@/VEService/Getstatus.do",kPublicInterfaceAPI] isPost:YES withParameters:@{@"Page":@"-1"}];
    [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
        NSDictionary *userInfo = response[0];
        self.onlineSatus =  [NSString stringWithFormat:@"%@",userInfo[@"onlineStaues"]];
        self.onlineSatus = @"1";
        if ([self.onlineSatus isEqualToString:@"1"]) {
            [button setTitle:@"分享挖矿+糖果" forState:(UIControlStateNormal)];
        }else{
             [button setTitle:@"分享" forState:(UIControlStateNormal)];
        }
        button.hidden = NO;
        NSString *htmlString = self.model.CONTENT;//这是html格式的代码段
        NSString *headerStr = @"<head><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'><style type='text/css'>img{max-width: 100%; width:720px!important; height: auto;}table{height: auto!important;width: 100%!important;max-width: 720px!important;margin: 0 auto!important; font-size: 14px!important;color:#151515!important;}tr td{height:28px!important;width:auto!important;margin:0 auto!important;}span{font-size:17px!important;}</style></head>";
        NSString *str = [NSString stringWithFormat:@"<html>%@<body>%@</body></html>",headerStr,htmlString];
        [webView loadHTMLString:str baseURL:nil];
        
    }];
    
   
    
    
  
    
    
    
    NSString *userId = [[CJUserInfoManager sharedManager] currentUserInfo].USERID;
    
    if (![NSObject isNilOrNull:userId]) {
        NSString *url = [NSString stringWithFormat:@"%@/VEService/AddCoinRecord.do",kPublicInterfaceAPI];
        
        NSDictionary *dic = @{
                              @"Userid": userId,
                              @"Articleid": self.model.ARTICLEID,
                              @"Cointype": @"阅读资讯",
                              @"Coinnumber": @"10"
                              };
        
        CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:url isPost:YES withParameters:dic];
        [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
            self.isSuccessNet = YES;
            if ([message isEqualToString:@"保存成功！"]) {
                [CJUIKit showWithString:@"阅读资讯奖励+10"];
            }else if ([message isEqualToString:@"只有第一次阅读和分享奖励糖果！"]){
                
            }else{
                [CJUIKit showWithString:message];
            }

        }];
        
    }
    

    
//    UILabel *lab = [UILabel new];
//    [self.view addSubview:lab];
//
//    lab.numberOfLines = 0;
//    NSMutableAttributedString *attrStr =  [[NSMutableAttributedString alloc] initWithData:[self.model.CONTENT dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
//    [attrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16.0] range:NSMakeRange(0, attrStr.length)];
//
//    lab.attributedText = attrStr;
}

- (void)shareAction {
    
    
    if (![[CJUserInfoManager sharedManager] isLogin]) {
        DTLogInViewController *vc = [[DTLogInViewController alloc] init];
        vc.loginSuccessBlock = ^{
        };
        [self pushVc:vc];
        
    } else {
        
        
//        http://150.109.69.137:8080/content.html?articleid=&vecode=
        
        NSString *VECODE = [[CJUserInfoManager sharedManager] currentUserInfo].VECODE;
        NSString *str = [NSString stringWithFormat:@"%@/content.html?articleid=%@&vecode=%@",kPublicInterfaceAPI,self.model.ARTICLEID,VECODE];
        [[shareFunctionCJ shareFunction] shareInfoP:self.view viewController:self WithUrlSTr:str WithTitle:self.model.ARTICLETITLE withImageViewUrlStr:self.model.ARTICLEMSG withDetailTitleString:@"分享资讯与快讯+50" withType:0];
        [shareFunctionCJ shareFunction].ShareWXSuccessBlock = ^{
            NSString *userId = [[CJUserInfoManager sharedManager] currentUserInfo].USERID;
            
            if (![NSObject isNilOrNull:userId]) {
                NSString *url = [NSString stringWithFormat:@"%@/VEService/AddCoinRecord.do",kPublicInterfaceAPI];
                
                NSDictionary *dic = @{
                                      @"Userid": userId,
                                      @"Articleid": self->_model.ARTICLEID,
                                      @"Cointype": @"分享资讯",
                                      @"Coinnumber": @"50"
                                      };
                
                CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:url isPost:YES withParameters:dic];
                [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
                    if ([message isEqualToString:@"保存成功！"]) {
                        [CJUIKit showWithString:@"分享资讯奖励+50"];
                    }else if ([message isEqualToString:@"只有第一次阅读和分享奖励糖果！"]){
                        
                    }else{
                        [CJUIKit showWithString:message];
                    }
                }];
                
            }
        };
    }
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
